using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
namespace Easislides
{
	internal class ImageCanvas : Control
	{
		private Image _image;
		private int _PosLeft = 0;
		private int _PosTop = 0;
		private int _PosWidth = 0;
		private int _PosHeight = 0;
		private string _FileName = "";
		private float _ImageRatio = 1f;
		private bool _PowerPoint = false;
		private int _SlideNumber = 0;
		private bool _ImageDone = false;
		public float BorderWidthFactor = 0f;
		public int PPSlideNumbering = 0;
		public bool ExternalPP = false;
		public int CurSelectedSlide = 0;
		public Image image
		{
			get
			{
				return this._image;
			}
			set
			{
				this._image = value;
			}
		}
		public int PosLeft
		{
			get
			{
				return this._PosLeft;
			}
			set
			{
				this._PosLeft = value;
			}
		}
		public int PosTop
		{
			get
			{
				return this._PosTop;
			}
			set
			{
				this._PosTop = value;
			}
		}
		public int PosWidth
		{
			get
			{
				return this._PosWidth;
			}
			set
			{
				this._PosWidth = value;
			}
		}
		public int PosHeight
		{
			get
			{
				return this._PosHeight;
			}
			set
			{
				this._PosHeight = value;
			}
		}
		public string FileName
		{
			get
			{
				return this._FileName;
			}
			set
			{
				this._FileName = value;
			}
		}
		public float ImageRatio
		{
			get
			{
				return this._ImageRatio;
			}
			set
			{
				this._ImageRatio = value;
			}
		}
		public bool PowerPoint
		{
			get
			{
				return this._PowerPoint;
			}
			set
			{
				this._PowerPoint = value;
			}
		}
		public int SlideNumber
		{
			get
			{
				return this._SlideNumber;
			}
			set
			{
				this._SlideNumber = value;
			}
		}
		public bool ImageDone
		{
			get
			{
				return this._ImageDone;
			}
			set
			{
				this._ImageDone = value;
			}
		}
		public void SetImageRatio(int InImageWidth, int InImageHeight)
		{
			this.ImageRatio = (float)InImageWidth / (float)InImageHeight;
		}
		public void ResizeCanvas(int InCanvasWidth, int InCanvasHeight)
		{
			base.Width = InCanvasWidth;
			base.Height = InCanvasHeight;
			if (this.ImageDone && this._image != null)
			{
				float num = (float)base.Width / (float)base.Height;
				this.ImageRatio = (float)this._image.Width / (float)this._image.Height;
				if (num < this.ImageRatio)
				{
					this.PosLeft = 0;
					this.PosWidth = base.Width;
					this.PosHeight = (int)((float)this.PosWidth / this.ImageRatio);
					this.PosTop = (base.Height - this.PosHeight) / 2;
				}
				else
				{
					this.PosHeight = base.Height;
					this.PosTop = 0;
					this.PosWidth = (int)((float)this.PosHeight * this.ImageRatio);
					this.PosLeft = (base.Width - this.PosWidth) / 2;
				}
				this.ImageDone = true;
			}
		}
		public ImageCanvas()
		{
			base.SetStyle(ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
			this.Cursor = Cursors.Hand;
			base.Visible = false;
			this.FileName = "";
			this.ImageRatio = 1f;
		}
		protected override void OnPaint(PaintEventArgs e)
		{
			if (this._image != null || !(this.FileName == ""))
			{
				if (base.Visible)
				{
					if (this.ImageDone)
					{
						this.Draw_image(e);
					}
					else
					{
						if (this.Calc_Image())
						{
							this.Draw_image(e);
						}
					}
				}
			}
		}
		private void Draw_image(PaintEventArgs e)
		{
			e.Graphics.Clear(this.BackColor);
			e.Graphics.DrawImage(this._image, new Rectangle(this._PosLeft, this._PosTop, this._PosWidth, this._PosHeight), new Rectangle(0, 0, this._image.Width, this._image.Height), GraphicsUnit.Pixel);
		}
		public void BuildNewImageThumbs(int NewCanvasIndex, int NewWidth, int NewHeight, ref string[] NewImageArray, int NewTotalCount, string NewPrefix, int NewCurSelectedSlide, Color NewBackColour, ToolTip NewToolTip, bool NewExternalPP)
		{
			try
			{
				int pPSlideNumbering = NewCanvasIndex + 1;
				string text = NewImageArray[NewCanvasIndex];
				base.Width = NewWidth;
				base.Height = NewHeight;
				this.ExternalPP = NewExternalPP;
				this.CurSelectedSlide = NewCurSelectedSlide;
				string caption;
				if (this.PowerPoint)
				{
					caption = this.PPSlideNumbering.ToString();
					this.SlideNumber = this.PPSlideNumbering;
				}
				else
				{
					caption = gf.GetDisplayNameOnly(ref NewImageArray[NewCanvasIndex], false, true);
					this.SlideNumber = 0;
					if (gf.Right(text, 4) == ".ppt")
					{
						text = string.Concat(new string[]
						{
							gf.ExtPPrefix,
							gf.ExtPPrefix_Num.ToString(),
							"\\",
							Path.GetFileNameWithoutExtension(text),
							".jpg"
						});
					}
					if (this.FileName == text && !this.ExternalPP)
					{
						this.ResizeCanvas(base.Width, base.Height);
						return;
					}
				}
				NewToolTip.SetToolTip(this, caption);
				this.PPSlideNumbering = pPSlideNumbering;
				this.BackColor = NewBackColour;
				this.BorderWidthFactor = 0.04f;
				this.ImageDone = false;
				this.FileName = text;
				base.Visible = true;
				base.Invalidate();
			}
			catch
			{
			}
		}
		public bool Calc_Image()
		{
			int num = 0;
			int num2 = 0;
			int num3 = 0;
			int num4 = 0;
			int posWidth = this.PosWidth;
			int posHeight = this.PosHeight;
			bool result;
			try
			{
				if (this.ExternalPP)
				{
					gf.ExternalPPT.BuildOneFirstScreenDump(this.PPSlideNumbering);
				}
				Image image = Image.FromFile(this.FileName);
				this.SetImageRatio(image.Width, image.Height);
				gf.CalcImageToFit(this.ImageRatio, base.Width, base.Height, ref num, ref num2, true, ref num3, ref num4);
				if (num <= 0 | num2 <= 0)
				{
					result = false;
					return result;
				}
				Image image2 = new Bitmap(num3, num4, PixelFormat.Format24bppRgb);
				Graphics graphics = Graphics.FromImage(image2);
				Rectangle rect = new Rectangle(0, 0, num3, num4);
				if (this.PowerPoint)
				{
					Font font = new Font("Microsoft Sans Serif", (float)num3 / 12f);
					int num5 = (int)((num3 >= num4) ? ((float)num3 * this.BorderWidthFactor) : ((float)num4 * this.BorderWidthFactor));
					Rectangle rect2 = new Rectangle(num5, num5, num3 - num5 * 2, num4 - num5 * 2);
					graphics.DrawImage(image, rect2);
					string text = this.PPSlideNumbering.ToString();
					PointF pointF = new PointF((float)(num5 + 2), (float)(num5 + 2));
					SizeF sizeF = graphics.MeasureString(text, font);
					graphics.FillRectangle(new SolidBrush(gf.BlackScreenColour), pointF.X + 3f, pointF.Y + 3f, sizeF.Width, sizeF.Height);
					graphics.FillRectangle(new SolidBrush(Color.Yellow), pointF.X, pointF.Y, sizeF.Width, sizeF.Height);
					graphics.DrawString(text, font, new SolidBrush(gf.BlackScreenColour), pointF.X, pointF.Y);
					if (this.CurSelectedSlide == this.PPSlideNumbering)
					{
						graphics.DrawRectangle(new Pen(new SolidBrush(Color.Red), (float)((int)font.Size)), rect);
					}
					else
					{
						graphics.DrawRectangle(new Pen(new SolidBrush(base.Parent.BackColor), (float)((int)font.Size)), rect);
					}
				}
				else
				{
					if (this.ExternalPP)
					{
						Font font = new Font("Microsoft Sans Serif", (float)num3 / 12f);
						int num5 = (int)((num3 >= num4) ? ((float)num3 * this.BorderWidthFactor) : ((float)num4 * this.BorderWidthFactor));
						Rectangle rect2 = new Rectangle(num5, num5, num3 - num5 * 2, num4 - num5 * 2);
						graphics.DrawImage(image, rect2);
						if (this.CurSelectedSlide == this.PPSlideNumbering)
						{
							graphics.DrawRectangle(new Pen(new SolidBrush(Color.Red), (float)((int)font.Size)), rect);
						}
						else
						{
							graphics.DrawRectangle(new Pen(new SolidBrush(this.BackColor), (float)((int)font.Size)), rect);
						}
					}
					else
					{
						graphics.DrawImage(image, rect);
					}
				}
				this.image = image2;
				this.ImageDone = true;
				this.ResizeCanvas(base.Width, base.Height);
			}
			catch
			{
				this.ImageDone = false;
				result = false;
				return result;
			}
			result = true;
			return result;
		}
	}
}
